package com.xzy.dao.impl;

import com.xzy.dao.IOrderDao;
import com.xzy.domain.Order;
import com.xzy.utils.jdbcUtils;
import java.sql.Connection;
import java.util.List;

public class OrderDaoImpl extends BaseDao<Order> implements IOrderDao {

    @Override
    public void addOrder(Order order) {
        Connection conn = jdbcUtils.getConnection();
        String sql = "insert into t_order(order_id,create_time,Price,status,user_id) value(?,?,?,?,?)";
        this.update(conn,sql,order.getOrder_id(),order.getCreate_time(),order.getPrice(),order.getStatus(),order.getUser_id());
        //此时不提交(此dao有事务),应该留到事务统一处理后提交
    }

    @Override
    public Order queryByOrderid(String orderId) {

        Connection conn = jdbcUtils.getConnection();
        String sql = "select order_id,create_time,Price,status,user_id from t_order where order_id=?";
        Order order = this.queryForOne(conn, sql, orderId);
        jdbcUtils.clossAndCommit();//因为获取连接时设置了手动提交所以必须调用该方法(提交并关闭)否则就会连接超时(数据库操作后一直不提交)
        return order;
    }

    @Override
    public List<Order> queryByUserid(Integer userId) {


        Connection conn = jdbcUtils.getConnection();
        String sql = "select order_id,create_time,Price,status,user_id from t_order where user_id=?";
        List<Order> orders = this.queryForList(conn, sql, userId);
        jdbcUtils.clossAndCommit();//因为获取连接时设置了手动提交所以必须调用该方法(提交并关闭)否则就会连接超时(数据库操作后一直不提交)
        return orders;
    }

    @Override
    public void updataStatus(String id, Integer status) {
        Connection conn = jdbcUtils.getConnection();
        String sql = "update t_order set status=? where order_id=?";
        this.update(conn,sql,status,id);
        jdbcUtils.clossAndCommit();//因为获取连接时设置了手动提交所以必须调用该方法(提交并关闭)否则就会连接超时(数据库操作后一直不提交)

    }

    @Override
    public List<Order> queryAll() {
        Connection conn = jdbcUtils.getConnection();
        String sql = "select order_id,create_time,Price,status,user_id from t_order";
        List<Order> orders = this.queryForList(conn, sql);
        jdbcUtils.clossAndCommit();//因为获取连接时设置了手动提交所以必须调用该方法(提交并关闭)否则就会连接超时(数据库操作后一直不提交)
        return orders;
    }
}
